<template>
	<view class="card1">
		<input type="text" v-model="input" placeholder="请输入手机号或邮箱..." placeholder-class="text" />
		<view class="get-code" :style="{'color':getCodeBtnColor}" @click.stop="getCode()">{{getCodeText}}</view>
	</view>
</template>

<script>
	import * as Utils from "../../store/index.js"
	import Valid from "../../config/valid.js"
	import config from '../../config/config.js'
	export default {
			data() {
				return {
					input:"",
					getCodeText:"获取验证码",
					getCodeBtnColor:"rgb(174 174 174)",
					getCodeWaiting:false,
					register:"register",
					format:"",
					lst:[],
					type:'1',
				}
			},
			methods: {
				Timer(){},
				getCode(){
					uni.hideKeyboard()
					if(this.getCodeWaiting){
						return;
					}
					const check=Valid.check(this.lst)
					if(check.check_rule('PhoneFormat',this.input)){
						this.format="phone"
					}
					if(check.check_rule('EmailFormat',this.input)){
						this.format="email"
					}
					uni.setStorage({
						key: 'format',
						data: this.format,
					})
					console.log(this.input)
					console.log(this.format)
					if(this.format===""){
						uni.showToast({
							title:'手机号或邮箱填写有误',
							icon:"none"
						});
						return false;
					}
					this.getCodeText="发送中"
					this.getCodeWaiting=true;
					this.getCodeBtnColor="rgb(174 174 174)"
					
					var page=uni.getStorageSync('cur_page')
					console.log(page)
					if(page==='register'){
						this.type='0'
					}
					console.log(this.type)
					console.log(this.input)
					setTimeout(()=>{
						// uni.showToast({
						// 	title:"已发送验证码",
						// 	icon:"success"
						// });
						this.$request.TokenRequest({
							url: '/user/verify',
						},{type:this.type,[this.format]:this.input}).then(res=>{
							console.log(res)
							console.log(res.data.detail)
							if(this.type==='0'){
								// console.log(11111111111111111111111)
								if(res.message==="手机号已被注册"||res.message==="邮箱已被注册"){
									this.getCodeText="获取验证码";
									this.getCodeWaiting=false;
									uni.setStorageSync('already_reg','1')
									uni.setStorageSync('id',null)
									uni.setStorage(
									// {
									// 	key:'already_reg',
									// 	value:'1'
									// },
									{
										key: 'correct_code',
										data: null,
									},
									{
										key:'number',
										data:{[this.format]:this.input}
									})
									uni.showToast({
										icon:'none',
										title:"联系方式已被注册"
									})
									console.log(uni.getStorageSync('already_reg'))
									
								}
								else{
									uni.setStorage({
										key: 'correct_code',
										data: res.data.code,},
										{
											key:'number',
											data:{[this.format]:this.input}
										})
									uni.showToast({
										title:"已发送验证码",
										icon:"success"
									});
									uni.setStorageSync('already_reg','0')
									uni.setStorageSync('id',res.data.id)
									this.setTimer();
								}
							}
							else if(res.data.detail==="邮箱未注册"||res.data.detail==="手机号未注册"){
								this.getCodeText="获取验证码";
								this.getCodeWaiting=false;
								uni.setStorageSync('already_reg','2')
								uni.setStorageSync('id',null)
								uni.setStorage(
								// {
								// 	key:'already_reg',
								// 	value:'1'
								// },
								{
									key: 'correct_code',
									data: null,
								},
								{
									key:'number',
									data:{[this.format]:this.input}
								})
								uni.showToast({
									icon:'none',
									title:"联系方式未注册"
								})
								console.log(uni.getStorageSync('already_reg'))
								
							}
							
							else{
								uni.setStorage({
									key: 'correct_code',
									data: res.data.code,},
									{
										key:'number',
										data:{[this.format]:this.input}
									})
								uni.showToast({
									title:"已发送验证码",
									icon:"success"
								});
								uni.setStorageSync('already_reg','0')
								uni.setStorageSync('id',res.data.id)
								this.setTimer();
							}
							// uni.setStorage({
							// 	key: 'correct_code',
							// 	data: res.data.code,},
							// 	{
							// 		key:'number',
							// 		data:{[this.format]:this.input}
							// 	},
							// 	{
							// 		key:'id',
							// 		data:res.data.id
							// 	})
						})
						// uni.request({
						// 	header:{
						// 		'X-Requested-With': 'XMLHttpRequest',
						// 		'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						// 	},
						// 	url: "http://" + config.url + '/user/register',
						// 	method:'post'
						// }).then(res=>{
						// 	console.log(res)
						// 	uni.setStorage({
						// 		key: 'correct_code',
						// 		data: res.data.code,})
						// })
						
						// this.code='1234';//默认1234
						// this.setTimer();
					},1000)
				},
				setTimer(){
					let holdTime=60;
					this.getCodeText="重新获取（60）"
					this.Timer=setInterval(()=>{
						if(holdTime<=0){
							this.getCodeWaiting=false;
							this.getCodeBtnColor="rgb(174 174 174)"
							this.getCodeText="获取验证码"
							clearInterval(this.Timer);
							return;
						}
						this.getCodeText="重新获取("+holdTime+")"
						holdTime--;
					},1000)
				},
			}
		}
	
</script>

<style lang="scss" scoped>

		.card1 {
			display: flex;
			background-color: #F5F5F5;
			margin: 50rpx auto;
			padding: 35rpx 55rpx;
			border-radius: 200rpx;
			
			// .text {
			// 	font-size: 35rpx;
			// }
			.get-code {
					position: absolute;
					height: 24rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					right: 0;
					padding: 12rpx 90rpx 15rpx 15rpx;
					z-index: 3;
					font-size: 25rpx;
					
					&:after { //点击以后，左边出现白色的线
						content: " ";
						width: 1upx; //宽度为1upx
						height: 50upx; //高度为50upx
						background-color: #A5A5A5;  //背景颜色为白色
						position: absolute;
						z-index: 3;
						margin-right: 100%;
						left: 0;
						top: 0;
					}
				}
		}
</style>

